//
//  CategoryDetailViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/22.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "CategoryDetailViewController.h"

#import "CategoryDetailCollectionViewCell.h"//商品CollectionViewCell

#import "GoodsDetailViewController.h"//商品详情页Controller

@interface CategoryDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIImageView *priceArrow;//价格箭头
@property (weak, nonatomic) IBOutlet UILabel *priceeee;

@end

@implementation CategoryDetailViewController
{
    UICollectionView *_goodsCollectionView;
    NSMutableArray *_goodsDataArray;
    
    BOOL _isDescending;//价格排序,默认降序
}

 static NSString *const CategoryDetailCollectionViewCellID = @"CategoryDetailCollectionViewCell";

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
    //网络请求
    [self network];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isDescending = YES;
    
    
    //初始化数据源
    [self createDataArray];
    //创建CollectionView
    [self createCollectionView];
}
- (void)createDataArray{
    _goodsDataArray = [NSMutableArray array];
    NSArray *arr = @[@[@"O.P.I.", @"假装是零食", @"599"], @[@"C.P.U.", @"假装是零食", @"599"], @[@"O.P.I.", @"假装是零食", @"599"], @[@"C.P.U.", @"假装是零食", @"599"], @[@"O.P.I.", @"假装是零食", @"599"], @[@"C.P.U.", @"假装是零食", @"599"], @[@"O.P.I.", @"假装是零食", @"599"], @[@"C.P.U.", @"假装是零食", @"599"], @[@"O.P.I.", @"假装是零食", @"599"], @[@"C.P.U.", @"假装是零食", @"599"]];
    [_goodsDataArray addObjectsFromArray:arr];
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 99, WIDTH, HEIGHT - 99 - 44) collectionViewLayout:layout];
    _goodsCollectionView.delegate = self;
    _goodsCollectionView.dataSource = self;
    _goodsCollectionView.showsVerticalScrollIndicator = NO;
    _goodsCollectionView.backgroundColor = BackgroundCOLOR;
    [self.view addSubview:_goodsCollectionView];
    
    [_goodsCollectionView registerNib:[UINib nibWithNibName:@"CategoryDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CategoryDetailCollectionViewCellID];
    
}


#pragma mark === collectionViewDelegate ===
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 3 * 2 + 80);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoryDetailCollectionViewCellID forIndexPath:indexPath];
    
    cell.imageView.backgroundColor = [UIColor colorWithRed:VALUE green:VALUE blue:VALUE alpha:1];
    cell.title.text = _goodsDataArray[indexPath.item][0];
    cell.detail.text = _goodsDataArray[indexPath.item][1];
    cell.price.text = _goodsDataArray[indexPath.item][2];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld  %@", indexPath.item, _goodsDataArray[indexPath.item][0]);
    GoodsDetailViewController *goodsDetail = [[GoodsDetailViewController alloc] init];
    goodsDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:goodsDetail animated:YES];
}



//上新
- (IBAction)new:(id)sender {
    NSLog(@"上新");
    [_goodsCollectionView reloadData];
}
//热销
- (IBAction)hot:(id)sender {
    NSLog(@"热销");
    [_goodsCollectionView reloadData];
}
- (IBAction)price:(id)sender {
    _isDescending = !_isDescending;
    NSLog(@"价格");
    if (_isDescending) {
        NSLog(@"降序");
    }else{
        NSLog(@"升序");
    }
    [_goodsCollectionView reloadData];
}
- (IBAction)filter:(id)sender {
    NSLog(@"筛选");
}

#pragma mark === 网络请求 ===
- (void)network{
    
}
@end
